// 用户模块 （token）
import { setData, getData, delData } from '@/utils/ls'
import { userAccountLogin } from '@/api/user'
const RABBITKEY = 'rabbit-key'
export default {
  namespaced: true,
  // 定义数据
  state: {
    profile: getData(RABBITKEY) || {}
  },
  // 同步的修改方法
  mutations: {
    setToken (state, profile) {
      state.profile = profile
      // 存入ls
      setData(RABBITKEY, profile)
    },
    removeToken (state) {
      state.profile = {}
      delData(RABBITKEY)
    }
  },
  actions: {
    // 异步方法
    async fetchLogin (ctx, { account, password }) {
      // 调用登录接口获取token
      const res = await userAccountLogin({ account, password })
      // 提交mutation存入vuex
      ctx.commit('setToken', res.result)
    }
  }
}
